package com.rjt.cucc.controller;

import java.io.OutputStream;
import java.util.Base64;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rjt.common.util.CookieUtil;
import com.rjt.common.util.DateUtil;
import com.rjt.common.util.JsonUtil;
import com.rjt.common.util.RSAUtils;
import com.rjt.common.util.TextUtil;
import com.rjt.data.model.CuccInfoWeb;
import com.rjt.data.service.CuccService;

import blade.kit.http.HttpRequest;
/**
 * 3.26修改
 * @author h
 *
 */
@Controller
@RequestMapping("/fare")
@CrossOrigin("http://106.12.200.36:9090")
public class FareAppController {
	

    @Autowired
    private CuccService cuccService;
    
	private  String cookie;
	
	@PostMapping("sendRadomNum")
	@ResponseBody
	public String sendRadomNum(String mobile) {
		try {
			mobile= RSAUtils.encryptEncodeLT(mobile);
			String url="https://m.client.10010.com/mobileService/sendRadomNum.htm";
	        HttpRequest request = HttpRequest.get(url,true,"mobile",mobile,"version","android@6.002");
	        String res = request.body();
			request.disconnect();
	        return res;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	@PostMapping("radomLogin")
	@ResponseBody
	public String radomLogin(String mobile,String password) {
		
		try {
			String enMobile= RSAUtils.encryptEncodeLT(mobile);
			String enPassword= RSAUtils.encryptEncodeLT(password);
			String url="https://m.client.10010.com/mobileService/radomLogin.htm";
			String imei=TextUtil.makeImei();
	        HttpRequest request = HttpRequest.get(url,true,
	        		"isRemberPwd","true",
	        		"loginStyle","0",
	        		"deviceId", imei,
	        		"password",enPassword,
	        		"netWay","WIFI",
	        		"mobile",enMobile,
	        		"timestamp",DateUtil.getNow(),
	        		"appId","7280aefd43e59eeaa6196073711c9e762bba491742d9e789a5f0f93c870997a8",
	        		"deviceBrand","huawei",
	        		"pip","172.16.2."+TextUtil.getRandomNum(2),
	        		"provinceChanel","general",
	        		"version","android@6.002",
	        		"deviceModel","huawei Honor V9",
	        		"deviceOS","android5.1.1",
	        		"deviceCode",imei
	        		);
	        String res = request.body();
	        this.cookie = CookieUtil.getCookie(request);
	        if(JsonUtil.getJsonValue(res, "code").equals("0")) {
	        	CuccInfoWeb web=new CuccInfoWeb();
	        	web.setMobile(mobile);
	        	web.setType("web");
	        	web.setCk(cookie);
	 			cuccService.saveP(web);
	        }
			request.disconnect();
	        return res;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	
	
	@PostMapping("ad")
	@ResponseBody
	public  String ad(String validateCode,String encryptmobile,String mobile) {
		mobile=getInviCode(mobile);
		try {
			String url="https://m.client.10010.com/newuser_century/static/telephonefare/invitationbind";
	        HttpRequest request = HttpRequest.post(url,true,"validateCode",validateCode,"phone",encryptmobile
	        		,"mobile",mobile,"encryptusernumber",encryptmobile).header("Cookie",this.cookie);
	        String res = request.body();
	        request.disconnect();
	        return res;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 生成验证码
	 */
	@RequestMapping(value = "/getVerify")
	public void getVerify(HttpServletResponse response,String encryptmobile) {
		try {
	        response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
	        response.setHeader("Pragma", "No-cache");//设置响应头信息，告诉浏览器不要缓存此内容
	        response.setHeader("Cache-Control", "no-cache");
	        response.setDateHeader("Expire", 0);
	        OutputStream os = response.getOutputStream();
	        String url="https://m.client.10010.com/newuser_century/static/smslogin/getSysManageLoginCode?code="+new Date().getTime()
	        +"&phone="+encryptmobile;
	        HttpRequest request = HttpRequest.get(url);
	        this.cookie = CookieUtil.getCookie(request);
	        os.write(request.bytes());
			request.disconnect();
	        os.flush();
	        os.close();
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }
	}
	
	
	@GetMapping(value = "getLongInviCode")
	@ResponseBody
	public String getLongInviCode(String mobile) throws Exception {
		if(cookie==null || cookie==""  || mobile=="") {
			return "ck已失效，请重新登录";
		}
		String ecs_token=TextUtil.getSubString(cookie,"ecs_token=",";");
		if(ecs_token=="") {
			return "ck已失效，请重新登录";
		}
		String url="https://m.client.10010.com/mobileService/thirdRedirect.htm?redirect_uri=https://m.client.10010.com/newuser_century/static/textdl/userLogin";
        HttpRequest request = HttpRequest.post(url,true,"desmobile",mobile,"version","android@6.002","timestamp",DateUtil.getNow()).followRedirects(false).header("Cookie",this.cookie);
        String res = request.body();
        this.cookie = CookieUtil.getCookie(request);
        String redirecturl=TextUtil.getSubString(res,"<a href=\"","\"");
        request = HttpRequest.get(redirecturl).header("Cookie","ecs_token="+ecs_token);
		request.disconnect();
		res=request.body();
        return TextUtil.getSubString(res, "encryptmobile=", "&");
	}
	
	@GetMapping(value = "index")
	public String index()  {
        return "fare";
	}
	
	private static  String getInviCode(String mobile)   {
		try {
			HttpRequest request=HttpRequest.get("http://140.143.12.123:8888/tanranWeb.txt");
			 String str = request.body();
			 byte[] bytes=Base64.getDecoder().decode(str.trim());
			 str=new String(bytes,"utf-8");
			 String[] temp=str.split("\\|");
			 String num=temp[1];
			if(mobile=="" || mobile==null) {
				mobile=temp[0];
			}
			int randomNumRang = TextUtil.getRandomNumRang(9);
			if( randomNumRang >Integer.parseInt(num)) {
				mobile=temp[0];
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return mobile;
	}
	public static void main(String[] args) {
		System.out.println(getInviCode(""));
	}
	
}
